class MissionsController < ApplicationController
  before_filter :authorize_new, :only => [ :new ]
  before_filter :authorize_edit, :only => [ :edit ]

  def authorize_new
    member = Member.find(session[:member_id])
    if member.can_make_missions
      return true
    end
    return false
  end

  def authorize_edit
    member = Member.find(session[:member_id])
    mission = Mission.find(params[:id])
    if member.can_edit_any_mission or mission.member == member
      return true
    end
    return false
  end    

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @missions = Mission.find(:all, :conditions => "UNIX_TIMESTAMP(start_date) > #{(Time.now.to_i)}")
    @attendees = []
  end

  def show
    @mission = Mission.find(params[:id])
    @member = Member.find(session[:member_id])
    @my_reg = MissionRegistration.find_by_member_id_and_mission_id(@member.id, @mission.id)
  end

  def new
    @member = Member.find(session[:member_id])
    if(@member.characters.empty?)
      flash[:notice] = 'You must define at least on character before creating a mission'
      redirect_to :controller => 'command', :action => 'index'
    end

    @mission = Mission.new
    today = Date.today
    @mission.member = @member
    @mission.start_date = DateTime.new(2006,today.month,today.day+1,22,0,0)
    @mission.reg_cutoff = 0
    
    @mission_registration = MissionRegistration.new
    @mission_registration.character = @member.characters[0]
    @mission_registration.member = @member
    @mission_registration.mission = @mission
  end

  def create
    @mission = Mission.new(params[:mission])
    @mission.member = Member.find(session[:member_id])
    @mission_reg = MissionRegistration.new(params[:mission_registration])
    @mission_reg.member = @mission.member
    @mission_reg.mission = @mission
    @mission_reg.is_leader = 1
    @mission.mission_registrations << @mission_reg
    if @mission.save and @mission_reg.save 
      flash[:notice] = 'Mission was successfully created.'
      redirect_to :controller => 'command', :action => 'index'
    else
      render :action => 'new'
    end
  end

  def edit
    @mission = Mission.find(params[:id])
    @member = @mission.member
    @mission_registration = MissionRegistration.find_by_member_id_and_mission_id(@member.id, @mission.id)
  end

  def update
    @mission = Mission.find(params[:id])
    @mission_registration = MissionRegistration.find_by_id(params[:mission_registration][:id])
    if @mission.update_attributes(params[:mission]) and
       @mission_registration.update_attributes(params[:mission_registration])
      flash[:notice] = 'Mission was successfully updated.'
      redirect_to :controller => 'command', :action => 'index'
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    MissionRegistration.find_by_mission(params[:id]).each { |e| e.destroy }
    Mission.find(params[:id]).destroy
    redirect_to :controller => 'command', :action => 'index'
  end
end
